package com.hongdee.base.imp.service.impl;

import com.hongdee.base.entity.PropertyVo;
import com.hongdee.base.imp.dao.IDataEntityPropertyDao;
import com.hongdee.base.imp.entity.TDataEntity;
import com.hongdee.base.imp.entity.TDataEntityProperty;
import com.hongdee.base.imp.service.IDataEntityPropertyService;
import com.hongdee.common.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 名  称:DataEntityPropertyServiceImpl
 * 类  型: class
 * 描  述: 用一句话描述类的作用
 * 路  径:com.hongdee.base.imp.service.impl
 * 作  者:My
 * 日  期:2017/2/15 10:57
 */
@Service
public class DataEntityPropertyServiceImpl implements IDataEntityPropertyService {
    private static final String ENTITY_TABELE = "T_DATA_ENTITY_PROPERTY";
    @Autowired
    private IDataEntityPropertyDao dataEntityPropertyDao;

    /**
     * 根据实体 ID，获取对应的属性字段名称
     *
     * @param entityId
     * @param clsName
     * @return
     */
    @Override
    public List<TDataEntityProperty> initPropertyList(String entityId, String clsName) throws Exception {
        List<PropertyVo> pvList = EntityUtils.getExcelDataList(clsName);

        if (null == pvList)
            throw new Exception("无可导入的属性");

        List<TDataEntityProperty> tdepList = new ArrayList<TDataEntityProperty>();
        for (int i = 0; i < pvList.size(); i++) {
            tdepList.add(new TDataEntityProperty(entityId, pvList.get(i)));
        }
        return tdepList;
    }



    @Override
    public Map page(String start, String limit, String entityId) throws Exception {
        StringBuffer sqlBuffer = new StringBuffer();
        sqlBuffer.append("select * from " + ENTITY_TABELE + " where Entity_ID = ? ");
        return this.dataEntityPropertyDao.page(sqlBuffer.toString(), new Object[]{entityId}, start, limit);
    }

    @Override
    public void saveOrUpdate(List<TDataEntityProperty> tdpList) throws Exception {
        dataEntityPropertyDao.saveOrUpdateList(tdpList);

    }

    /**
     * 更新属性信息
     *
     * @param tdp
     * @throws Exception
     */
    @Override
    public void saveOrUpdateProp(TDataEntityProperty tdp) throws Exception {

        this.dataEntityPropertyDao.saveOrUpdate(tdp);

    }



    /**
     * 根据 实体 ID，删除记录
     *
     * @param objId
     * @return
     */
    @Override
    public boolean clearById(String objId) throws Exception {
        String hql = "delete from TDataEntityProperty where entityId = :id";
        Map params = new HashMap();
        params.put("id", objId);
        this.dataEntityPropertyDao.update(hql, params);
        return true;
    }
}
